package com.ruoyi.common.utils.sms;

import com.alibaba.fastjson2.JSONObject;
import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.tea.TeaException;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;

// 文档参考地址 https://next.api.aliyun.com/api-tools/sdk/Dysmsapi?version=2017-05-25&language=java-tea&tab=primer-doc

/**
 * @desc  阿里云发送验证码
 * @author fwx 2024/1/18 11:15
 **/
public class AliyunSmsService {

    public static final String accessKeyId = "LTAI4FkkEhjRQSpvJGrBj98z";
    public static final String accessKeySecret = "tiFVgHD2ydONFXb2ORIe8dPJNl3Wtm";

    public static Client smsClient = null;

    /**
     * 使用AK&SK初始化账号Client
     */
    static {
        try {
            Config config = new Config()
                    .setAccessKeyId(accessKeyId) // 必填，您的 AccessKey ID
                    .setAccessKeySecret(accessKeySecret); // 必填，您的 AccessKey Secret

            // Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi
            config.endpoint = "dysmsapi.aliyuncs.com";
            smsClient = new Client(config);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * @desc  发送注册验证码
     * @author fwx 2024/1/19 9:07
     **/
    public static void sendRegisterCode(String phone, String code){
        JSONObject json = new JSONObject();
        json.put("code",code);
        sendCode(phone,json,"SMS_183750550");
    }

    /**
     * @desc  发送重置验证码
     * @author fwx 2024/1/19 9:07
     **/
    public static void sendResetCode(String phone, String code){
        JSONObject json = new JSONObject();
        json.put("code",code);
        sendCode(phone,json,"SMS_183750549");
    }

    /**
     * @desc  虫情始见预警
     * @author fwx 2024/3/27 11:42
     **/
    public static void sendInsectFirst(String phone, JSONObject json){
        String[] split = phone.split(",");
        for (String pho : split) {
             sendCode(pho,json,"SMS_465318340");
        }
    }

    /**
     * @desc  虫情日高峰预警
     * @author fwx 2024/3/27 11:42
     **/
    public static void sendInsectDay(String phone, JSONObject json){
        String[] split = phone.split(",");
        for (String pho : split) {
             sendCode(pho,json,"SMS_465318591");
        }
    }

    /**
     * @desc  虫情月高峰预警
     * @author fwx 2024/3/27 11:42
     **/
    public static void sendInsectMonth(String phone, JSONObject json){
        String[] split = phone.split(",");
        for (String pho : split) {
             sendCode(pho,json,"SMS_465333184");
        }
    }

    /**
     * @desc  灾情预警
     * @author fwx 2024/3/28 16:07
     **/
    public static void sendFactorWarning(String phone, JSONObject json){
        String[] split = phone.split(",");
        for (String pho : split) {
            sendCode(pho,json,"SMS_465422467");
        }
    }
    /**
     * @desc  发送短信
     * @author fwx 2024/1/18 13:42
     **/
    public static void sendCode(String phone, JSONObject json,String templateCode){

        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setTemplateCode(templateCode)
                .setPhoneNumbers(phone)
                .setTemplateParam(json.toJSONString())
                .setSignName("比昂科技");

        try {
            // 复制代码运行请自行打印 API 的返回值
            SendSmsResponse sendSmsResponse = smsClient.sendSmsWithOptions(sendSmsRequest, new RuntimeOptions());
            System.err.println(String.format("[%s]发送成功短信完成,状态码为[%d],发送结果为[%s] ",phone,sendSmsResponse.getStatusCode(),sendSmsResponse.body.getMessage() ) );
        } catch (TeaException error) {
            // 错误 message
            System.out.println(error.getMessage());
            // 诊断地址
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 错误 message
            System.out.println(error.getMessage());
            // 诊断地址
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
        }
    }

    public static void main(String[] args_) throws Exception {



    }
}

